#include "Geometrie.h"
//Punct
Punct::Punct()
{
    this->x=NULL;
    this->y=NULL;
}
Punct::Punct (double x, double y)
{
    this->x=x;
    this->y=y;
}
double Punct::getX()
{
    return this->x;
}
double Punct::getY()
{
    return this->y;
}
void Punct::setX(double x)
{
    this->x=x;
}
void Punct::setY(double y)
{
    this->y=y;
}
Punct Punct::operator+(Punct p)
{
    double x,y;
    x=this->x+p.getX();
    y=this->y+p.getY();
    Punct s=Punct(x,y);
    return s;
}
Punct Punct::operator-(Punct p)
{
    double x,y;
    x=this->x-p.getX();
    y=this->y-p.getY();
    Punct s=Punct(x,y);
    return s;
}
Punct Punct::operator*(Punct p)
{
    double x,y;
    x=this->x*p.getX();
    y=this->y*p.getY();
    Punct s=Punct(x,y);
    return s;
}
Punct Punct::operator/(Punct p)
{
    double x,y;
    x=this->x/p.getX();
    y=this->y/p.getY();
    Punct s=Punct(x,y);
    return s;
}
bool Punct::operator==(Punct p)
{
    if((this->x==p.getX())&&this->y==p.getY()) return true;
    else return false;
}
bool Punct::operator!=(Punct p)
{
    if((this->x==p.getX())&&this->y==p.getY()) return false;
    else return true;
}

ostream& operator<<(ostream& afiseaza, const Punct& pct)
{
    afiseaza <<"("<< pct.x<<", "<<pct.y<<")";
    return afiseaza;
}

//Segment

Punct Segment::getA()
{
    return this->A;
}

Punct Segment::getB()
{
    return this->B;
}

void Segment::setA(Punct A)
{
    this->A=A;
}

void Segment::setB(Punct B)
{
    this->B=B;
}


double Segment::calcLungime()
{
    double xd,yd;
    xd=this->getB().getX()-this->getA().getX();
    yd=this->getB().getY()-this->getA().getY();
    return sqrt(xd*xd+yd*yd);
}

Segment::Segment(Punct A, Punct B)
{
   this->A=A;
   this->B=B;
}

//Poligon

Poligon::Poligon (Punct puncte[100])
{
    for (int i=0;i<100;i++)
    this->puncte[i]=puncte[i];
}
Punct Poligon::getPunct(int k)
{
    return this->puncte[k];
}

int Poligon::nrPuncte()
{
    Punct p=Punct(0,0);
    int i;
    for (i=1;i<100 && this->puncte[i]!=p && (this->puncte[i]!=this->puncte[0]);i++);
    return i;
}

bool Poligon::isPoligon()
{
    int k=this->nrPuncte();
    if (this->puncte[0]==this->puncte[k]) return true;
    return false;
}
bool Poligon::isTriunghi()
{
    int k=this->nrPuncte();
    if (k==3 && (this->isPoligon())) return true;
    else return false;
}
bool Poligon::isParalelogram()
{
    int k=this->nrPuncte();

    if (k==4 && (this->isPoligon()))
    {
    Segment ab=Segment(this->puncte[0],this->puncte[1]);
    Segment bc=Segment(this->puncte[1],this->puncte[2]);
    Segment cd=Segment(this->puncte[2],this->puncte[3]);
    Segment da=Segment(this->puncte[3],this->puncte[0]);
    if ((ab.calcLungime()==cd.calcLungime()) && (bc.calcLungime()==da.calcLungime())) return true;
    else return false;
    }
    else return false;
}
bool Poligon::isDreptunghi()
{
    if (this->isParalelogram())
    {
    Segment ac=Segment(this->puncte[0],this->puncte[2]);
    Segment bd=Segment(this->puncte[1],this->puncte[3]);
    if(ac.calcLungime()==bd.calcLungime()) return true;
    else return false;
    }
    else return false;
}
bool Poligon::isPatrat()
{
    if (this->isDreptunghi())
    {
    Segment ab=Segment(this->puncte[0],this->puncte[1]);
    Segment bc=Segment(this->puncte[1],this->puncte[2]);
    if(ab.calcLungime()==bc.calcLungime()) return true;
    else return false;
    }
    else return false;
}
bool Poligon::isRomb()
{
    if (this->isParalelogram())
    {
    Segment ab=Segment(this->puncte[0],this->puncte[1]);
    Segment bc=Segment(this->puncte[1],this->puncte[2]);
    if(ab.calcLungime()==bc.calcLungime()) return true;
    else return false;
    }
    else return false;
}
bool Poligon::isConvex()
{
    if (this->isPoligon())
    {
        int k=this->nrPuncte();
        int i;
        Punct mij=Punct();
        for (i=0;i<k-1;i++)
            {
                Segment diag=Segment(this->puncte[i],this->puncte[i+2]);
                mij.setX((diag.getA().getX()+diag.getB().getX())/2);
                mij.setY((diag.getA().getY()+diag.getB().getY())/2);
                for (i=0;i<k;i++)
                {
                    Segment curent=Segment(this->puncte[i],this->puncte[i+1]);
                    if(((mij.getX()-curent.getA().getX())/(curent.getB().getX()-curent.getA().getX()))!=((mij.getY()-curent.getA().getY())/(curent.getB().getY()-curent.getA().getY()))) return false;
                }
            }
    }
    return true;
}
